﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DBService.Service
{
    public class PicService : RohonModels.IModels.IPic, IDisposable
    {
        internal DBService.DBManager db;

        public PicService()
        {
            db = new DBManager();
        }

        public int Insert(RohonModels.Models.Pic pic)
        {
            db.Pic.Add(pic);
            return Convert.ToInt32(pic.Id);
        }

        public bool isExist(string picMd5, out RohonModels.Models.Pic pic)
        {
            pic = null;
            bool isExist = false;
            var query = db.Pic.Where(a => a.MD5.Equals(picMd5));
            if (query.Count() > 0)
            {
                isExist = true;
                pic = query.First();
            }
            return isExist;
        }

        public void Dispose()
        {
            db.SaveChanges();
            db.Dispose();
        }


        public bool hasDownloaded(string url)
        {
            var query = db.Pic.Where(a => a.URL.ToLower().Equals(url.ToLower()));
            if (query.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
